package com.nd.mobile.study.view;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.domain.BluebookRecord;
import com.nd.mobile.study.domain.VideoRecord;
import com.nd.mobile.study.view.adapter.SimpleListAdapter;

public class BluebookRecordCatalogActivity extends BaseActivity{

	private ListView mLV; 
	private BluebookRecordListAdapter mAdapter; 
	
	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.simple_list);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		loadList();
	}

	@Override
	protected void initViews() {
		mLV = (ListView)findViewById(R.id.common_lv);
	}
	
	private void loadList() {
		mLoading.p();
		new GetBluebookRecordTask(this).execute();
	}
	
	private class GetBluebookRecordTask extends SimpleAsyncTask<Void, Void, BluebookRecord[]> {

		public GetBluebookRecordTask(Context context) {
			super(context);
		}

		@Override
		protected BluebookRecord[] onLoad(Void... params) throws Exception {
			return App.getInstance().getUser().getBluebookRecord();
		}

		@Override
		protected void onUI(BluebookRecord[] result) {
			mAdapter = new BluebookRecordListAdapter(mContext, result);
			mLV.setAdapter(mAdapter);
			mAdapter.notifyDataSetChanged();
		}
	}
	
	private class BluebookRecordListAdapter extends SimpleListAdapter<BluebookRecord>{

		public BluebookRecordListAdapter(Context context, List<BluebookRecord> data) {
			super(context, data);
		}

		public BluebookRecordListAdapter(Context context, BluebookRecord[] data) {
			super(context, data);
		}

		@Override
		public boolean isEnabled(int position) {
			return false;
		}
		
		private class ViewHolder {
			TextView	tvTitle;
			TextView	tvBeginTime;
			TextView	tvEndTime;
			TextView	tvScore;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(null == convertView){
				convertView = mInflater.inflate(R.layout.bluebook_record_list_item, null);
				
				holder = new ViewHolder();
				holder.tvTitle = (TextView) convertView.findViewById(R.id.bluebook_record_catalog_title);
				holder.tvBeginTime = (TextView) convertView.findViewById(R.id.bluebook_record_catalog_begin_time);
				holder.tvEndTime = (TextView) convertView.findViewById(R.id.bluebook_record_catalog_end_time);
				holder.tvScore = (TextView) convertView.findViewById(R.id.bluebook_record_catalog_score);
				
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			holder.tvTitle.setText(mData.get(position).getName());
			holder.tvBeginTime.setText("开始:"+mData.get(position).getBeginTime());
			holder.tvEndTime.setText("结束:"+mData.get(position).getEndTime());
			holder.tvScore.setText("得分:"+mData.get(position).getScore());
			
			return convertView;
		}
	}
}
